package com.quanyan.sys.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.common.Plugin.Page;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.sys.entity.Dict;
import com.quanyan.sys.entity.DictExample;
import com.quanyan.sys.mapper.DictMapper;
import com.quanyan.sys.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author songzj
 * @date 16/4/25-15:07
 */
@Service
public class DictServiceImpl implements DictService {

    @Autowired
    private DictMapper dictMapper;

    @Override
    public APIResponse insert(Dict dict) {
        if (dict == null) {
            APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        if (dict.getParentId() == null || dict.getParentId() < 0) {
            dict.setParentId(0);
        }
        Date date = new Date();
        dict.setCreateTime(date);
        dict.setUpdateTime(date);
        this.dictMapper.insertSelective(dict);
        return APIResponse.returnSuccess(dict);
    }

    @Override
    public APIResponse update(Dict dict) {

        if (dict == null || dict.getId() == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }

        Dict persist = this.dictMapper.selectByPrimaryKey(dict.getId());
        if (persist == null) {
            return APIResponse.returnFail(ApiResponseEnum.RECORD_DO_NOT_EXISTS);
        }

        dict.setCreateTime(null);
        dict.setUpdateTime(new Date());
        this.dictMapper.updateByPrimaryKeySelective(dict);
        return APIResponse.returnSuccess(ApiResponseEnum.SUCCESS);
    }


    @Override
    public APIResponse updateDictEnable(Integer id) {
        return updateEnableState(id, Constants.IS_ENABLE_TRUE);
    }


    @Override
    public APIResponse updateDictDisable(Integer id) {
        return updateEnableState(id, Constants.IS_ENABLE_FALSE);
    }

    @Override
    public APIResponse deleteDict(Integer id) {
        Dict dict = new Dict();
        dict.setId(id);
        dict.setIsDelete(Constants.IS_DELETE_TRUE);
        dict.setUpdateTime(new Date());
        this.dictMapper.updateByPrimaryKeySelective(dict);

        //子类目.
        dict.setId(null);
        DictExample dictExample = new DictExample();
        DictExample.Criteria criteria = dictExample.createCriteria();
        criteria.andParentIdEqualTo(id);
        this.dictMapper.updateByExampleSelective(dict, dictExample);

        return APIResponse.returnSuccess(ApiResponseEnum.SUCCESS);
    }

    @Override
    public APIResponse<Dict> get(Integer id) {
        Dict dict = this.dictMapper.selectByPrimaryKey(id);
        return APIResponse.returnSuccess(dict);
    }

    @Override
    public APIResponse<Dict> get(String key) {

        DictExample dictExample = new DictExample();
        DictExample.Criteria criteria = dictExample.createCriteria();
        criteria.andDataKeyValueEqualTo(key);

        List<Dict> dicts = this.dictMapper.selectByExample(dictExample);
        if (dicts != null && dicts.size() > 0) {
            return APIResponse.returnSuccess(dicts.get(0));
        }

        return APIResponse.returnFail(ApiResponseEnum.RECORD_DO_NOT_EXISTS);
    }

    @Override
    public APIResponse<PageObj<List<Dict>>> selectDictsByKey(String key, int pageNum, int size) {
        Dict dict = this.get(key).getData();
        if (dict == null) {
            APIResponse.returnFail(ApiResponseEnum.RECORD_DO_NOT_EXISTS);
        }

        DictExample dictExample = new DictExample();
        DictExample.Criteria dictCriteria = dictExample.createCriteria();
        dictCriteria.andParentIdEqualTo(dict.getId());

        DictExample.Criteria keyCriteria = dictExample.createCriteria();
        keyCriteria.andDataKeyValueEqualTo(key);
        dictExample.or(keyCriteria);

        int total = this.dictMapper.countByExample(dictExample);

        Page page = Page.createPage(total, pageNum, size);
        dictExample.setPage(page);
        List<Dict> dicts = this.dictMapper.selectByExample(dictExample);

        if (dicts.size() == 0){
          return   APIResponse.returnFail(ApiResponseEnum.DO_NOT_HAVE_ANY_MORE_RECORD);
        }

        PageObj<List<Dict>> list = PageObj.create(dicts, page);
        return APIResponse.returnSuccess(list);
    }

    @Override
    public APIResponse<PageObj<List<Dict>>> selectDictsByExample(Dict example, int pageNum, int size) {

        DictExample dictExample = new DictExample();
        DictExample.Criteria criteria = dictExample.createCriteria();

        if (example.getId() != null && example.getId().intValue() > 0) {
            criteria.andIdEqualTo(example.getId());
        }

        if (!StringUtil.isBlankOrNull(example.getDataKeyValue())) {
            criteria.andDataKeyValueLike("%" + example.getDataKeyValue() + "%");
        }

        if (example.getParentId() != null && example.getParentId() >= 0) {
            criteria.andParentIdEqualTo(example.getParentId());
        }

        if (example.getIsEnable() != null) {
            criteria.andIsEnableEqualTo(example.getIsEnable());
        }

        if (example.getEditAble() != null) {
            criteria.andEditAbleEqualTo(example.getEditAble());
        }

        int count = this.dictMapper.countByExample(dictExample);

        Page page = Page.createPage(count, pageNum, size);
        dictExample.setPage(page);
        List<Dict> dicts = this.dictMapper.selectByExample(dictExample);
        if (dicts.size() == 0){
            return APIResponse.returnFail(ApiResponseEnum.DO_NOT_HAVE_ANY_MORE_RECORD);
        }

        PageObj<List<Dict>> pageObj = PageObj.create(dicts, page);

        return APIResponse.returnSuccess(pageObj);
    }

    private APIResponse updateEnableState(Integer id, Byte enableState) {
        Dict persist = this.dictMapper.selectByPrimaryKey(id);
        if (persist == null || persist.getIsDelete().byteValue() == Constants.IS_DELETE_TRUE) {
            return APIResponse.returnFail(ApiResponseEnum.RECORD_DO_NOT_EXISTS);
        }

        //父类目
        Dict update = new Dict();
        update.setId(id);
        update.setIsEnable(enableState);
        update.setUpdateTime(new Date());
        this.dictMapper.updateByPrimaryKeySelective(update);

        //更新子类目.
        DictExample dictExample = new DictExample();
        DictExample.Criteria dictCriteria = dictExample.createCriteria();
        dictCriteria.andParentIdEqualTo(id);

        update.setId(null);
        this.dictMapper.updateByExampleSelective(update, dictExample);
        return APIResponse.returnSuccess(ApiResponseEnum.SUCCESS);
    }
}
